﻿using System;
using System.IO;
using System.Collections.Generic;

namespace AIC_Notification
{
    static class Global
    {
        static public Configuration config;

        static public StudentDB SOF_StudentDB = null;
        static public StudentDB Guidance_StudentDB = null;
        static public IDictionary<string, SOF_Application> SOF_Applications;
        static public IDictionary<string, Guidance_Application> Guidance_Applications;
        static public IList<Error> InputErrors;
        static public Utility SysUtil;
        static public IList<FormatedReport> Reports = null;

        // Settings
        static public bool ReparseStudentID = true;
        static public bool ProcessTaskList = false;
        static public bool SendReportAsEmail = false;
        static public int LogDisplayLevel = 0;
        static public int WaitUntilStart = 15;

        // Directory Configurations
        static public string InputDirectory;
        static public string OutputDirectory;
        static public string TempDirectory;
        static public string LogPathFile = "";
        static public string ErrorPathFile = "";

        // Context for Error Logging
        static public AIC_Application ProcessingApplication = null;
        static public AIC_List ProcessingWorkList = null;

        static private string Program = "";
        static private DateTime StartDateTime = DateTime.MinValue;
        static private DateTime RealStartDateTime = DateTime.MinValue;

        static public void Initialize()
        {
            // Global objects
            config = new Configuration();
            SOF_Applications = new Dictionary<string, SOF_Application>();
            Guidance_Applications = new Dictionary<string, Guidance_Application>();
            InputErrors = new List<Error>();

            Reports = new List<FormatedReport>();

            // Setup Key Directory
            InputDirectory = ".\\input\\";
            if (!Directory.Exists(Global.InputDirectory))
                Directory.CreateDirectory(Global.InputDirectory);

            TempDirectory = ".\\temp\\";
            if (!Directory.Exists(Global.TempDirectory))
                Directory.CreateDirectory(Global.TempDirectory);
        }

        static public bool InitializeDataSource()
        {
            SOF_StudentDB = new StudentDB(ApplicationType.sof);
            if (!SOF_StudentDB.Initialize())
                return false;

            Guidance_StudentDB = new StudentDB(ApplicationType.guidance);
            if (!Guidance_StudentDB.Initialize())
                return false;

            return true;
        }

        static public DateTime Now()
        {
            if (config.IsSetDateTime)
            {
                if (StartDateTime == DateTime.MinValue)
                {
                    StartDateTime = config.SetDateTime;
                    RealStartDateTime = DateTime.Now;
                }

                return StartDateTime + (DateTime.Now - RealStartDateTime);
            }
            else
                return DateTime.Now;
        }

        static public void SetProgram()
        {
            // Set Program String
            if (Statistics.ApplicationType == ApplicationType.sof)
                Program = "SOF";
            else if (Statistics.ApplicationType == ApplicationType.guidance)
                Program = "Guidance";
            else
                Program = "";
        }

        static public void SetOutputDirectory()
        {
            // Set and create a new output directory
            for (int i = 1; i < 99; i++)
            {
                Global.OutputDirectory = String.Format(".\\output\\{0}\\{1:yyyyMMdd}-{2:D2}\\", Program, Global.Now(), i);
                if (!Directory.Exists(Global.OutputDirectory))
                {
                    Directory.CreateDirectory(Global.OutputDirectory);
                    break;
                }
            }

            Global.LogPathFile = String.Format("{0}AIC-Log-{1:yyyyMMdd-HHmmss}.txt", Global.OutputDirectory, Global.Now());
            Global.ErrorPathFile = String.Format("{0}AIC-Error-{1:yyyyMMdd-HHmmss}.txt", Global.OutputDirectory, Global.Now());
        }
    }
}
